{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T15:12:57.852264Z",
     "start_time": "2025-08-18T15:12:42.934920Z"
    }
   },
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "llm = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    api_key=os.getenv(\"DASH_SCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model=\"qwen-plus\",\n",
    "    temperature=0.8,\n",
    "    max_tokens=60,\n",
    ")\n",
    "\n",
    "template = \"\"\"\n",
    "你是一个植物学家。给定花的名称和类型，你需要为这种花写一个200字左右的介绍。\n",
    "花名: {name}\n",
    "颜色: {color}\n",
    "植物学家: 这是关于上述花的介绍:\"\"\"\n",
    "\n",
    "from langchain.chains import LLMChain, SequentialChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"name\", \"color\"],\n",
    "    template=template\n",
    ")\n",
    "introduction_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"introduction\"\n",
    ")\n",
    "\n",
    "# 第二个LLMChain：根据鲜花的介绍写出鲜花的评论\n",
    "template = \"\"\"\n",
    "你是一位鲜花评论家。给定一种花的介绍，你需要为这种花写一篇200字左右的评论。\n",
    "鲜花介绍:\n",
    "{introduction}\n",
    "花评人对上述花的评论:\"\"\"\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"introduction\"],\n",
    "    template=template\n",
    ")\n",
    "review_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"review\"\n",
    ")\n",
    "\n",
    "# 第三个LLMChain：根据鲜花的介绍和评论写出一篇自媒体的文案\n",
    "template = \"\"\"\n",
    "你是一家花店的社交媒体经理。给定一种花的介绍和评论，你需要为这种花写一篇社交媒体的帖子，300字左右。\n",
    "鲜花介绍:\n",
    "{introduction}\n",
    "花评人对上述花的评论:\n",
    "{review}\n",
    "社交媒体帖子:\n",
    "\"\"\"\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"introduction\", \"review\"],\n",
    "    template=template\n",
    ")\n",
    "social_post_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt_template,\n",
    "    output_key=\"social_post_text\"\n",
    ")\n",
    "\n",
    "# 总的链：按顺序运行三个链\n",
    "overall_chain = SequentialChain(\n",
    "    chains=[introduction_chain, review_chain, social_post_chain],\n",
    "    input_variables=[\"name\", \"color\"],\n",
    "    output_variables=[\"introduction\", \"review\", \"social_post_text\"],\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "# 运行链并打印结果\n",
    "result = overall_chain({\n",
    "    \"name\": \"玫瑰\",\n",
    "    \"color\": \"黑色\"\n",
    "})\n",
    "print(result)"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\49247\\AppData\\Local\\Temp\\ipykernel_20576\\2159507171.py:25: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
      "  introduction_chain = LLMChain(\n",
      "C:\\Users\\49247\\AppData\\Local\\Temp\\ipykernel_20576\\2159507171.py:75: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
      "  result = overall_chain({\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001B[1m> Entering new SequentialChain chain...\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "{'name': '玫瑰', 'color': '黑色', 'introduction': '黑色玫瑰是一种独特而神秘的花卉，其深邃的颜色通常由特定品种的基因决定，或是通过特殊的栽培技术、染色处理获得。自然界的“黑玫瑰”实际上呈现为深红或紫红色调，在光线照射下呈现出近似黑色的效果。这类玫瑰多用于园艺观赏，具有浓郁的香气与优雅的形态。在文化象征中，黑色玫瑰常代表神秘、告别或重生，是艺术与仪式中的重要意象。', 'review': '黑色玫瑰，这一神秘而富有深意的花卉，以其独特的深邃色彩挑战着人们对玫瑰的传统认知。它并非自然的叛逆者，而是光影与基因交织的奇迹。在暮色中，它如夜般沉静；在阳光下，又流露出隐秘的红晕，仿佛藏着不为人知的故事。它不为取悦而生，却更像一位沉默的思想者，带着告别与重生的寓意，悄然述说着生命的轮回。无论是作为花艺设计中的点睛之笔，还是仪式场合中的情感寄托，黑色玫瑰都以其不可复制的气质，赢得了属于它的尊崇。它不是终结，而是另一种开始。', 'social_post_text': '【神秘绽放，黑玫瑰的深意之美 🌹🖤】\\n\\n你是否也被这抹深邃的黑所吸引？黑色玫瑰，不为取悦而来，只为讲述一个关于告别与重生的故事。它不是传统意义上的浪漫象征，而更像是光影与基因交织出的奇迹，低调却充满力量。\\n\\n在阳光下，它流露出隐隐的红晕，仿佛藏着不为人知的秘密；在暮色中，它沉静如夜，神秘而从容。它不争不抢，却总能在花艺设计中成为最动人的那一笔。\\n\\n无论是作为礼物，还是仪式中的情感寄托，黑色玫瑰都在用自己的方式诠释生命的轮回与新的开始。\\n\\n来店里看看这朵“沉默思想者”吧，或许你会从中读出属于自己的意义。#黑玫瑰 #花语 #生命之美 #独特花艺\\n\\n（300字左右）'}\n"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
